package ca.etsmtl.log121.tp3.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import ca.etsmtl.log121.tp3.controller.ApplicationController;
import ca.etsmtl.log121.tp3.listener.MarkerListener;
import ca.etsmtl.log121.tp3.model.MarkerModel;
import ca.etsmtl.log121.tp3.view.ThumbnailView;


public class ApplicationFrame extends JFrame {
	/**
	 * TODO
	 */
	private static final long serialVersionUID = -5422398828705335350L;

	public static final String APPLICATION_NAME = "PerspectiveImageViewer";

	private static final int PREFERRED_WIDTH = 900;
	private static final int PREFERRED_HEIGHT = 700;

	private static final Dimension SPACER_DIMENSION = new Dimension(2, 2);

	private final ApplicationController controller;

	private final MenuManager menu;

	private final JPanel mainPanel;

	private JPanel thumbnailPanel;

	private ManageableTabbedPane tabbedPane;

	public ApplicationFrame() {
		super(APPLICATION_NAME);
		setSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		controller = new ApplicationController(this);

		// Menu bar
		menu = new MenuManager(this);

		// Main panel
		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());

		rebuildEmptyWorkspace();

		// Container panel (use for space marging)
		JPanel containerPanel = new JPanel();
		containerPanel.setLayout(new BorderLayout());
//		containerPanel.add(Box.createRigidArea(SPACER_DIMENSION), BorderLayout.NORTH);
		containerPanel.add(Box.createRigidArea(SPACER_DIMENSION), BorderLayout.EAST);
		containerPanel.add(Box.createRigidArea(SPACER_DIMENSION), BorderLayout.WEST);
		containerPanel.add(Box.createRigidArea(SPACER_DIMENSION), BorderLayout.SOUTH);
		containerPanel.add(mainPanel, BorderLayout.CENTER);

		add(containerPanel);
	}

	public void resetWorkspace() {
		mainPanel.removeAll();
		mainPanel.revalidate();
		mainPanel.repaint();

		menu.rebuildMarkersMenu();

		thumbnailPanel = null;
		tabbedPane = null;
	}

	public void rebuildEmptyWorkspace() {
		resetWorkspace();

		JLabel label = new JLabel("Open an image");
		label.setToolTipText("Open an image");

		JPanel emptyPanel = new JPanel();
		emptyPanel.add(label);

		mainPanel.add(Box.createRigidArea(new Dimension(1, 50)), BorderLayout.NORTH);
		mainPanel.add(emptyPanel, BorderLayout.CENTER);
	}

	public void rebuildWorkspace(ThumbnailView thumbnail) {
		resetWorkspace();

		// Thumbnail panel
		thumbnailPanel = new JPanel();
		thumbnailPanel.add(thumbnail);

		// Tabbed pane
		tabbedPane = new ManageableTabbedPane(this);

		mainPanel.add(thumbnailPanel, BorderLayout.WEST);
		mainPanel.add(tabbedPane, BorderLayout.CENTER);
	}

	public void refresh() {
		menu.refresh();
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public JPanel getThumbnailPanel() {
		return thumbnailPanel;
	}

	public ManageableTabbedPane getTabbedPane() {
		return tabbedPane;
	}

	public ApplicationController getController() {
		return controller;
	}

	public void addMakerToMenu(MarkerModel maker) {
		JMenuItem makerMarkersItem = new JMenuItem(maker.getName());
		makerMarkersItem.addActionListener(new MarkerListener(maker, controller));

		menu.addMarker(makerMarkersItem);
	}
}
